package com.situ.student;

import java.util.List;
import java.util.Scanner;

public class Mangement implements Functions {

	Scanner sc = new Scanner(System.in);
	StudentDmo sd = new StudentDmo();

	public void start() {

		while (true) {
			System.out.println("------学生档案管理-----");
			System.out.println("1.插入学生信息");
			System.out.println("2.查看全部学生信息");
			System.out.println("3.查询学生信息");
			System.out.println("4.修改学生信息");
			System.out.println("5.删除学生信息");
			System.out.println("6.退出系统");
			System.out.println("请输入：");
			switch (Integer.parseInt(sc.nextLine())) {
			case 1:
				insert();
				break;
			case 2:
				findAll();
				break;
			case 3:
				find();
				break;
			case 4:
				modify();
				break;
			case 5:
				delete();
				break;
			case 6:
				return;
			default:
				break;
			}
		}
	}

	/**
	 * 插入学生信息
	 */
	public void insert() {
		while (true) {
			System.out.println("-----添加学生信息-----");
			System.out.println("添加学生ID：");
			String Id = sc.nextLine();
			if (isExistId(Id)) {
				System.out.println("已存在学生ID，请重新输入！！");
				continue;
			}
			System.out.println("添加学生姓名：");
			String name = sc.nextLine();
			System.out.println("添加学生专业：");
			String major = sc.nextLine();
			Students student = new Students(Id, name, major);
			int rst = sd.insert(student);
			if (rst == 0) {
				System.out.println("录入失败，请重新录入");
			} else {
				System.out.println("录入成功！");
			}
			System.out.println("是否继续添加(y/n):");
			if (sc.nextLine().equals("n")) {
				break;
			}
		}
	}

	/**
	 * 插入学生信息子功能 -- 查找学生ID是否存在
	 * 
	 * @param Id
	 * @return
	 */
	public boolean isExistId(String Id) {
		List<Students> list = sd.selectList(new Students(Id, null, null));
		for (Students stu : list) {
			if (stu.getId().equals(Id)) {
				return true;
			}
		}
		return false;
	}

	public void findAll() {
		List<Students> list = sd.selectList(new Students(null, null, null));
		if (list == null) {
			System.out.println("未找到学生信息！！");
		}
		for (Students stu : list) {
			System.out.println("ID: " + stu.getId() + " name: " + stu.getName() + " 专业: " + stu.getMajor());
		}
	}

	/**
	 * 查询学生信息
	 */
	public void find() {
		while (true) {
			System.out.println("-----查找学生信息-----");
			System.out.println("1.按学号查找");
			System.out.println("2.按姓名查找");
			int in = Integer.parseInt(sc.nextLine());
			if (in == 1) {
				System.out.println("请输入学生学号：");
				String inputId = sc.nextLine();
				List<Students> list = sd.selectList(new Students(inputId, null, null));
				if (list == null) {
					System.out.println("未找到学生信息！！");
				}
				for (Students stu : list) {
					System.out.println("ID: " + stu.getId() + " name: " + stu.getName() + " 专业: " + stu.getMajor());
				}
			}
			if (in == 2) {
				System.out.println("请输入学生姓名：");
				String inputName = sc.nextLine();
				List<Students> list = sd.selectList(new Students(null, inputName, null));
				if (list == null) {
					System.out.println("未找到学生信息！！");
				}
				for (Students stu : list) {
					System.out.println("ID: " + stu.getId() + " name: " + stu.getName() + " 专业: " + stu.getMajor());
				}
			}
			System.out.println("是否继续查询(y/n):");
			if (sc.nextLine().equalsIgnoreCase("n")) {
				break;
			}
		}
	}

	/**
	 * 修改学生信息
	 */
	public void modify() {
		while (true) {
			System.out.println("-----修改学生信息----");
			System.out.println("输入要修改学生的ID:");
			String Id = sc.nextLine();
			System.out.println("输入修改后的姓名:");
			String name = sc.nextLine();
			System.out.println("输入修改后的专业:");
			String major = sc.nextLine();
			Students stu = new Students(Id, name, major);
			int rst = sd.update(stu);
			if (rst == 0) {
				System.out.println("修改失败，请重新修改");
			} else {
				System.out.println("修改成功！");
			}
			System.out.println("是否继续修改(y/n):");
			if (sc.nextLine().equalsIgnoreCase("n")) {
				break;
			}
		}
	}

	public void delete() {
		while (true) {
			System.out.println("----删除学生信息----");
			System.out.println("请输入要删除的学生的ID:");
			String Id = sc.nextLine();
			int rst = sd.deleteByCode(Id);
			if (rst == 0) {
				System.out.println("删除失败，请重新删除");
			} else {
				System.out.println("删除成功！");
			}
			System.out.println("是否继续删除(y/n):");
			if (sc.nextLine().equalsIgnoreCase("n")) {
				break;
			}
		}
	}
}
